package com.oasys.controller;

import com.oasys.entity.LoginUser;
import com.oasys.entity.MenuDto;
import com.oasys.entity.SysMenuEntity;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.MenuService;
import com.oasys.service.SysUserService;
import com.oasys.utils.ResponseResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/12
 **/
@RestController
@RequestMapping("/menuInfo")
@Slf4j
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 获取当前用户菜单
     * @param userId id
     * @return list
     */
    @GetMapping("/getMenus")
    public ResponseResult<List<MenuDto>> getMenus(Long userId){
        LoginUser loginUser = sysUserService.getLoginUser();
        List<MenuDto> menus = menuService.findMenusByUserId(loginUser.getUserId());
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,menus);
    }

    /**
     * 获取所有菜单
     * @return list
     */
    @GetMapping("/getAllMenus")
    public ResponseResult<List<MenuDto>> getAllMenus(){
        List<MenuDto> menus = menuService.getAllMenus();
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,menus);
    }

    /**
     * 超级管理员添加菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/addMenu")
    @ApiOperation("添加菜单")
    public ResponseResult<Boolean> addMenu(@RequestBody SysMenuEntity sysMenu){
        Boolean result = menuService.addMenu(sysMenu);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
    }

    @GetMapping("/getAllParentMenus")
    @ApiOperation("获取所有父级菜单")
    public ResponseResult<List<SysMenuEntity>> getAllParentMenus(@RequestParam String menuName){
        List<SysMenuEntity> allParentMenus = menuService.getAllParentMenus(menuName);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,allParentMenus);
    }

    @PostMapping("/addMenuOne")
    @ApiOperation("添加单个菜单-管理员账号才有此权限(admin)")
    public ResponseResult<Boolean> addMenuOne(@RequestBody SysMenuEntity sysMenu){
        log.info("菜单:{}",sysMenu);
        return menuService.addMenuOne(sysMenu);
    }
}
